package com.controller;

import com.model.kefu.Kefu;
import com.service.kefu.KefuService;
import com.thread.AccessTokenThread;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/kefu")
public class KefuController {
    private static Logger log = LoggerFactory.getLogger(KefuController.class);
    @Autowired
    private KefuService kefuService;


    //查询全部客服
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public String get(){

        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        if(at!=null){
            JSONObject jsonObject=kefuService.get(at);
            return String.valueOf(jsonObject);
        }else {
            log.error("查询客服失败");
            return "没数据";
        }
    }

    //添加客服
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void add(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        if(at!=null){
            kefuService.add(getKefu(),at);
        }else {
            log.error("添加客服失败");
        }
    }

    //编辑客服
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public void update(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        if(at!=null){
            kefuService.update(getKefu(),at);
        }else {
            log.error("编辑客服失败");
        }
    }

    //删除客服
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public void delete(){
        // 调用接口获取access_token
        String at = AccessTokenThread.accessToken.getToken();
        if(at!=null){
            kefuService.delete(deleteKefu(),at);
        }else {
            log.error("删除客服失败");
        }
    }

    //创建固定的客服账号
    public Kefu getKefu(){
        Kefu kefu=new Kefu();
        kefu.setKf_account("h7747449@gh_6eabab31ce16");
        kefu.setNickname("客服小8一号");
        kefu.setPassword("123");
        return kefu;
    }//编辑固定的客服账号
    public Kefu updateKefu(){
        Kefu kefu=new Kefu();
        kefu.setKf_account("h7747449@gh_6eabab31ce16");
        kefu.setNickname("客服小8一号");
        kefu.setPassword("123");
        return kefu;
    }//删除固定的客服账号
    public String deleteKefu(){
        String kf_account= "h7747449@gh_6eabab31ce16";

        return kf_account;
    }
}

